package com.manxi.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.manxi.constant.StatusConstant;
import com.manxi.context.BaseContext;
import com.manxi.dto.CategoryDTO;
import com.manxi.dto.CategoryPageQueryDTO;
import com.manxi.entity.Category;
import com.manxi.entity.Employee;
import com.manxi.mapper.CategoryMapper;
import com.manxi.result.PageResult;
import com.manxi.result.Result;
import com.manxi.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 分类分页查询
     *
     * @param categoryPageQueryDTO
     * @return
     */

    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增分类
     *
     * @param categoryDTO
     * @return
     */

    public void save(CategoryDTO categoryDTO) {
        //Category
        Category category = new Category();

        //对象属性拷贝
        BeanUtils.copyProperties(categoryDTO, category);

        //设置分类状态,默认正常状态 1表示正常 0表示锁定
        category.setStatus(StatusConstant.ENABLE);

//        //设置当前记录的创建时间和修改时间
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        //设置当前记录创建人id和修改人id
//        category.setCreateUser(BaseContext.getCurrentId());
//        category.setUpdateUser(BaseContext.getCurrentId());

        //调用Dao层插入数据
        categoryMapper.insert(category);
    }

    /**
     * 删除分类
     *
     * @param id
     * @return
     */

    public void delete(Long id) {
        categoryMapper.delete(id);
    }

    /**
     * 启售禁售分类
     *
     * @param status
     * @param id
     * @return
     */

    public void startOrStop(Integer status, Long id) {
        Category category = Category.builder()
                .status(status)
                .id(id)
                .build();
        categoryMapper.update(category);
    }

    /**
     * 修改分类信息
     * @param categoryDTO
     */
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(category);
    }


    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    public List<Category> list(Integer type) {
        return categoryMapper.list(type);
    }

}
